package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.modules.erp.domain.dto.InOutItemParam;
import com.xc.erp.modules.erp.domain.pojo.JshInOutItem;
import com.xc.erp.modules.erp.mapper.JshInOutItemMapper;
import com.xc.erp.modules.erp.service.JshInOutItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 收支项目
 */
@Service
public class JshInOutItemServiceImpl extends ServiceImpl<JshInOutItemMapper, JshInOutItem>
    implements JshInOutItemService {

    @Override
    public IPage<JshInOutItem> listPage(InOutItemParam inOutItemParam, Integer current, Integer pageSize) {
        IPage<JshInOutItem> page = new Page<>(current, pageSize);
        QueryWrapper<JshInOutItem> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(inOutItemParam.getName())){
            wrapper.like("name",inOutItemParam.getName());
        }
        if(!StringUtils.isEmpty(inOutItemParam.getType())){
            wrapper.eq("type",inOutItemParam.getType());
        }
        if(!StringUtils.isEmpty(inOutItemParam.getRemark())){
            wrapper.like("remark",inOutItemParam.getRemark());
        }
        wrapper.orderByAsc("sort");
        page = baseMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public boolean checkIsNameExist(String name) {
        JshInOutItem inOutItem = baseMapper.selectOne(new QueryWrapper<JshInOutItem>().eq("name", name));
        if(null != inOutItem){
            return true;
        }
        return false;
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void batchSetStatus(boolean status, Long[] ids) {
        List<JshInOutItem> supplierList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            JshInOutItem supplier = new JshInOutItem();
            supplier.setEnabled(status);
            supplier.setId(ids[i]);
            supplierList.add(supplier);
        }
        this.updateBatchById(supplierList);
    }
}




